/* Plot power/ROC curves.
*/

clear
set more off
set scheme  s1mono 

*Set working directory to rd graph folder:
cd "${main}"

cap log close
log using "${logs}/power_functions.log", replace

*Import Asymptotic variance data:
use "${dat}/asymptotic_variances_rdd.dta", clear
split filename, parse(_)
rename filename1 playerdgp

tempfile variances
save "`variances'"

* Import symmetry of DGP data
use "${dat}/dgp_symmetry.dta", clear 
tempfile symmetry
save "`symmetry'"

*Phase 1
use "${dat}/Pilot 1.0/survey_merged.dta", clear

merge m:1 playerdgp using "`variances'", nogen
merge m:1 playerdgp using "`symmetry'", nogen
destring playerdisc, replace
replace playerdisc = abs(playerdisc)
tab playerdisc

rename playerdisc disc

preserve
collapse (mean) y, by(playertreatment_group disc)
twoway (connected y disc if playertreatment_group == "A", msymbol(Oh)) ///
(connected y disc if playertreatment_group == "C", msymbol(Th)) ///
(connected y disc if playertreatment_group == "B", msymbol(Dh)) ///
(connected y disc if playertreatment_group == "D", msymbol(Sh)) ///
,legend(lab (1 "Small Bins, Normal Scale") lab(3 "Large Bins, Normal Scale") lab(2 "Small Bins, Large Scale") lab(4 "Large Bins, Large Scale"))  ///
ytitle("Share Reporting Discontinuity") xtitle("Discontinuity Magnitude")  title("Phase 1") ///
ymtick(0.05) ymlabel("0.05", labsize(small) angle(0)) ///
ylabel(, angle(0))
graph export "${output}/ROC_phase1.pdf", as(pdf) replace
restore

* By symmetry of DGP
preserve
keep if symmetric == 1
collapse (mean) y, by(playertreatment_group disc)
twoway (connected y disc if playertreatment_group == "A", msymbol(Oh)) ///
(connected y disc if playertreatment_group == "C", msymbol(Th)) ///
(connected y disc if playertreatment_group == "B", msymbol(Dh)) ///
(connected y disc if playertreatment_group == "D", msymbol(Sh)) ///
,legend(lab (1 "Small Bins, Normal Scale") lab(3 "Large Bins, Normal Scale") lab(2 "Small Bins, Large Scale") lab(4 "Large Bins, Large Scale"))  ///
ytitle("Share Reporting Discontinuity") xtitle("Discontinuity Magnitude") 
graph export "${output}/ROC_phase1_symmetric_supports.pdf", as(pdf) replace
restore
preserve
keep if symmetric == 0
collapse (mean) y, by(playertreatment_group disc)
twoway (connected y disc if playertreatment_group == "A", msymbol(Oh)) ///
(connected y disc if playertreatment_group == "C", msymbol(Th)) ///
(connected y disc if playertreatment_group == "B", msymbol(Dh)) ///
(connected y disc if playertreatment_group == "D", msymbol(Sh)) ///
,legend(lab (1 "Small Bins, Normal Scale") lab(3 "Large Bins, Normal Scale") lab(2 "Small Bins, Large Scale") lab(4 "Large Bins, Large Scale"))  ///
ytitle("Share Reporting Discontinuity") xtitle("Discontinuity Magnitude") 
graph export "${output}/ROC_phase1_asymmetric_supports.pdf", as(pdf) replace
restore


*By player DGP:
*DGP specific graphs:
levelsof playerdgp, local(levels) 
local count = 1
foreach l of local levels{
preserve
keep if playerdgp == "`l'"

collapse (mean) y, by(playertreatment_group disc)

twoway (connected y disc if playertreatment_group == "A", msymbol(Oh)) ///
(connected y disc if playertreatment_group == "C", msymbol(Th)) ///
(connected y disc if playertreatment_group == "B", msymbol(Dh)) ///
(connected y disc if playertreatment_group == "D", msymbol(Sh)), ///
ytitle("") xtitle("")  legend(lab (1 "Small Bins, Normal Scale") lab(3 "Large Bins, Normal Scale") lab(2 "Small Bins, Large Scale") lab(4 "Large Bins, Large Scale")) title("DGP `count'", size(medsmall)) ///
 name(graph`count', replace) 


local count = `count' + 1
restore
}
grc1leg graph1 graph2 graph3 graph4 graph5 graph6 graph7 graph8 graph9 graph10 graph11, legendfrom(`1') title("Phase 1")
graph export "${output}/ROC_phase1_byDGP.pdf", as(pdf) replace

/*===========================================================
Create ROC curves using rescaled x axis:
=============================================================*/
destring dgp_sd, replace

gen asymp_sd = sqrt(asymp_var_rd_est)
gen abs_disc_cv = disc*dgp_sd
gen x = abs_disc_cv / asymp_sd
*-------------------------------------------
*Creating binned version of combined graph

/*For the binned combined graph with x=axis restricted to 10, use 10 equally 
spaced dbins between 0 and 10.*/

count if x>0 & x<0.5
egen bins = cut(x), at(0 0.5(1)10.5)

*Obtain bin midpoints:
replace bins = bins + 0.5 if bins != . & bins ~= 0

preserve
keep if bins <=10
collapse (mean) y, by(playertreatment_group bins)
twoway (connected y bins if playertreatment_group == "A", msymbol(Oh)) ///
(connected y bins if playertreatment_group == "C", msymbol(Th)) ///
(connected y bins if playertreatment_group == "B", msymbol(Dh)) ///
(connected y bins if playertreatment_group == "D", msymbol(Sh)) ///
,legend(lab (1 "Small Bins, Normal Scale") lab(3 "Large Bins, Normal Scale") lab(2 "Small Bins, Large Scale") lab(4 "Large Bins, Large Scale"))  ///
ytitle("Share Reporting Discontinuity") xtitle("Rescaled Discontinuity (Binned)") title("Phase 1") ///
ymtick(0.05) ymlabel("0.05", labsize(small) angle(0)) ///
ylabel(, angle(0))
graph export "${output}/ROC_rescaled_phase1.pdf", as(pdf) replace
restore


*By player DGP:
*DGP specific graphs:
levelsof playerdgp, local(levels) 
local count = 1
foreach l of local levels{
preserve
keep if playerdgp == "`l'"

collapse (mean) y, by(playertreatment_group x)

twoway (connected y x if playertreatment_group == "A", msymbol(Oh)) ///
(connected y x if playertreatment_group == "C", msymbol(Th)) ///
(connected y x if playertreatment_group == "B", msymbol(Dh)) ///
(connected y x if playertreatment_group == "D", msymbol(Sh)), ///
ytitle("") xtitle("")  legend(lab (1 "Small Bins, Normal Scale") lab(3 "Large Bins, Normal Scale") lab(2 "Small Bins, Large Scale") lab(4 "Large Bins, Large Scale")) title("DGP `count'", size(medsmall)) ///
 name(graph`count', replace) 

local count = `count' + 1
restore
}
grc1leg graph1 graph2 graph3 graph4 graph5 graph6 graph7 graph8 graph9 graph10 graph11, legendfrom(`1') title("Phase 1")
graph export "${output}/ROC_phase1_rescaled_byDGP.pdf", as(pdf) replace

graph close _all

*Phase II
*------------------

use "${dat}/Phase II/survey_merged_PhaseII.dta", clear

merge m:1 playerdgp using "`variances'", nogen
merge m:1 playerdgp using "`symmetry'", nogen

destring playerdisc, replace
replace playerdisc = abs(playerdisc)
rename  playerdisc disc

preserve
collapse (mean) y, by(playertreatment_group disc)
twoway (connected y disc if playertreatment_group == "A", msymbol(Oh)) ///
(connected y disc if playertreatment_group == "C", msymbol(Th)) ///
(connected y disc if playertreatment_group == "B", msymbol(Dh)) ///
(connected y disc if playertreatment_group == "D", msymbol(Sh)) ///
,legend(lab (1 "Small Bins, Equal Spacing") lab(3 "Large Bins, Equal Spacing") lab(2 "Small Bins, Quantile Spacing") lab(4 "Large Bins, Quantile Spacing"))  ///
ytitle("Share Reporting Discontinuity") xtitle("Discontinuity Magnitude") title("Phase 2") ///
ymtick(0.05) ymlabel("0.05", labsize(small) angle(0)) ///
ylabel(, angle(0))
graph export "${output}/ROC_phase2.pdf", as(pdf) replace
restore

* By symmetry of support
preserve
keep if symmetric == 1
collapse (mean) y, by(playertreatment_group disc)
twoway (connected y disc if playertreatment_group == "A", msymbol(Oh)) ///
(connected y disc if playertreatment_group == "C", msymbol(Th)) ///
(connected y disc if playertreatment_group == "B", msymbol(Dh)) ///
(connected y disc if playertreatment_group == "D", msymbol(Sh)) ///
,legend(lab (1 "Small Bins, Equal Spacing") lab(3 "Large Bins, Equal Spacing") lab(2 "Small Bins, Quantile Spacing") lab(4 "Large Bins, Quantile Spacing"))  ///
ytitle("Share Reporting Discontinuity") xtitle("Discontinuity Magnitude") 
graph export "${output}/ROC_phase2_symmetric_supports.pdf", as(pdf) replace
restore
preserve
keep if symmetric == 0
collapse (mean) y, by(playertreatment_group disc)
twoway (connected y disc if playertreatment_group == "A", msymbol(Oh)) ///
(connected y disc if playertreatment_group == "C", msymbol(Th)) ///
(connected y disc if playertreatment_group == "B", msymbol(Dh)) ///
(connected y disc if playertreatment_group == "D", msymbol(Sh)) ///
,legend(lab (1 "Small Bins, Equal Spacing") lab(3 "Large Bins, Equal Spacing") lab(2 "Small Bins, Quantile Spacing") lab(4 "Large Bins, Quantile Spacing"))  ///
ytitle("Share Reporting Discontinuity") xtitle("Discontinuity Magnitude") 
graph export "${output}/ROC_phase2_asymmetric_supports.pdf", as(pdf) replace
restore

graph close _all

*By player DGP:
*DGP specific graphs:
levelsof playerdgp, local(levels) 
local count = 1
foreach l of local levels{
preserve
keep if playerdgp == "`l'"

collapse (mean) y, by(playertreatment_group disc)

twoway (connected y disc if playertreatment_group == "A", msymbol(Oh)) ///
(connected y disc if playertreatment_group == "C", msymbol(Th)) ///
(connected y disc if playertreatment_group == "B", msymbol(Dh)) ///
(connected y disc if playertreatment_group == "D", msymbol(Sh)), ///
ytitle("") xtitle("")  legend(lab (1 "Small Bins, Equal Spacing") lab(3 "Large Bins, Equal Spacing") lab(2 "Small Bins, Quantile Spacing") lab(4 "Large Bins, Quantile Spacing")) title("DGP `count'", size(medsmall)) ///
 name(graph`count', replace) 


local count = `count' + 1
restore
}
grc1leg graph1 graph2 graph3 graph4 graph5 graph6 graph7 graph8 graph9 graph10 graph11, legendfrom(`1') title("Phase 2")
graph export "${output}/ROC_phase2_byDGP.pdf", as(pdf) replace



/*===========================================================
Create ROC curves using rescaled x axis:
=============================================================*/
destring dgp_sd, replace

gen asymp_sd = sqrt(asymp_var_rd_est)
gen abs_disc_cv = disc*dgp_sd
gen x = abs_disc_cv / asymp_sd

*-------------------------------------------
*Creating binned version of combined graph

count if x>0 & x<0.5
egen bins = cut(x), at(0 0.5(1)10.5)

*Obtain bin midpoints:
replace bins = bins + 0.5 if bins != . & bins ~= 0

preserve
keep if bins<=10
collapse (mean) y, by(playertreatment_group bins)
twoway (connected y bins if playertreatment_group == "A", msymbol(Oh)) ///
(connected y bins if playertreatment_group == "C", msymbol(Th)) ///
(connected y bins if playertreatment_group == "B", msymbol(Dh)) ///
(connected y bins if playertreatment_group == "D", msymbol(Sh)) ///
,legend(lab (1 "Small Bins, Equal Spacing") lab(3 "Large Bins, Equal Spacing") lab(2 "Small Bins, Quantile Spacing") lab(4 "Large Bins, Quantile Spacing"))  ///
ytitle("Share Reporting Discontinuity") xtitle("Rescaled Discontinuity (Binned)") title("Phase 2") ///
ymtick(0.05) ymlabel("0.05", labsize(small) angle(0)) ///
ylabel(, angle(0))
graph export "${output}/ROC_rescaled_phase2.pdf", as(pdf) replace
restore

*By player DGP:
*DGP specific graphs:
levelsof playerdgp, local(levels) 
local count = 1
foreach l of local levels{
preserve
keep if playerdgp == "`l'"

collapse (mean) y, by(playertreatment_group x)

twoway (connected y x if playertreatment_group == "A", msymbol(Oh)) ///
(connected y x if playertreatment_group == "C", msymbol(Th)) ///
(connected y x if playertreatment_group == "B", msymbol(Dh)) ///
(connected y x if playertreatment_group == "D", msymbol(Sh)), ///
ytitle("") xtitle("")  legend(lab (1 "Small Bins, Equal Spacing") lab(3 "Large Bins, Equal Spacing") lab(2 "Small Bins, Quantile Spacing") lab(4 "Large Bins, Quantile Spacing")) title("DGP `count'", size(medsmall)) ///
 name(graph`count', replace) 


local count = `count' + 1
restore
}
grc1leg graph1 graph2 graph3 graph4 graph5 graph6 graph7 graph8 graph9 graph10 graph11, legendfrom(`1') title("Phase 2")
graph export "${output}/ROC_phase2_rescaled_byDGP.pdf", as(pdf) replace

graph close _all

*Phase 3
*-----------------

use "${dat}/Phase III/survey_merged_PhaseIII.dta", clear
merge m:1 playerdgp using "`variances'", nogen
merge m:1 playerdgp using "`symmetry'", nogen

replace playerdisc = abs(playerdisc)
rename playerdisc disc

preserve
collapse (mean) y, by(playertreatment_group disc)
twoway (connected y disc if playertreatment_group == "A", msymbol(Oh)) ///
(connected y disc if playertreatment_group == "B", msymbol(Dh)) ///
(connected y disc if playertreatment_group == "C", msymbol(Th)) ///
,legend(lab (1 "No Fit Line, Vertical Line") lab(2 "Fit Line, Vertical Line") lab(3 "No Fit Line, No Vertical Line"))  ///
ytitle("Share Reporting Discontinuity") xtitle("Discontinuity Magnitude") title("Phase 3") ///
ymtick(0.05) ymlabel("0.05", labsize(small) angle(0)) ///
ylabel(, angle(0))
graph export "${output}/ROC_phase3.pdf", as(pdf) replace
restore

* By symmetry of DGP support
preserve
keep if symmetric == 1
collapse (mean) y, by(playertreatment_group disc)
twoway (connected y disc if playertreatment_group == "A", msymbol(Oh)) ///
(connected y disc if playertreatment_group == "B", msymbol(Dh)) ///
(connected y disc if playertreatment_group == "C", msymbol(Th)) ///
,legend(lab (1 "No Fit Line, Vertical Line") lab(2 "Fit Line, Vertical Line") lab(3 "No Fit Line, No Vertical Line"))  ///
ytitle("Share Reporting Discontinuity") xtitle("Discontinuity Magnitude") 
graph export "${output}/ROC_phase3_symmetric_supports.pdf", as(pdf) replace
restore
preserve
keep if symmetric == 0
collapse (mean) y, by(playertreatment_group disc)
twoway (connected y disc if playertreatment_group == "A", msymbol(Oh)) ///
(connected y disc if playertreatment_group == "B", msymbol(Dh)) ///
(connected y disc if playertreatment_group == "C", msymbol(Th)) ///
,legend(lab (1 "No Fit Line, Vertical Line") lab(2 "Fit Line, Vertical Line") lab(3 "No Fit Line, No Vertical Line"))  ///
ytitle("Share Reporting Discontinuity") xtitle("Discontinuity Magnitude") 
graph export "${output}/ROC_phase3_asymmetric_supports.pdf", as(pdf) replace
restore

*By player DGP:
*DGP specific graphs:
levelsof playerdgp, local(levels) 
local count = 1
foreach l of local levels{
preserve
keep if playerdgp == "`l'"

collapse (mean) y, by(playertreatment_group disc)

twoway (connected y disc if playertreatment_group == "A", msymbol(Oh)) ///
(connected y disc if playertreatment_group == "B", msymbol(Dh)) ///
(connected y disc if playertreatment_group == "C", msymbol(Th)), ///
ytitle("") xtitle("")  legend(lab (1 "No Fit Line, Vertical Line") lab(2 "Fit Line, Vertical Line") lab(3 "No Fit Line, No Vertical Line")) title("DGP `count'", size(medsmall)) ///
 name(graph`count', replace) 


local count = `count' + 1
restore
}
grc1leg graph1 graph2 graph3 graph4 graph5 graph6 graph7 graph8 graph9 graph10 graph11, legendfrom(`1') title("Phase 3")
graph export "${output}/ROC_phase3_byDGP.pdf", as(pdf) replace


/*===========================================================
Create ROC curves using rescaled x axis:
=============================================================*/
destring dgp_sd, replace

gen asymp_sd = sqrt(asymp_var_rd_est)
gen abs_disc_cv = disc*dgp_sd
gen x = abs_disc_cv / asymp_sd
*-------------------------------------------
*Creating binned version of combined graph

count if x>0 & x<0.5
egen bins = cut(x), at(0 0.5(1)10.5)

*Obtain bin midpoints:
replace bins = bins + 0.5 if bins != . & bins ~= 0

preserve
keep if bins<=10
collapse (mean) y, by(playertreatment_group bins)
twoway (connected y bins if playertreatment_group == "A", msymbol(Oh)) ///
(connected y bins if playertreatment_group == "B", msymbol(Dh)) ///
(connected y bins if playertreatment_group == "C", msymbol(Th)) ///
,legend(lab (1 "No Fit Line, Vertical Line") lab(2 "Fit Line, Vertical Line") lab(3 "No Fit Line, No Vertical Line"))  ///
ytitle("Share Reporting Discontinuity") xtitle("Rescaled Discontinuity (Binned)") title("Phase 3") ///
ymtick(0.05) ymlabel("0.05", labsize(small) angle(0)) ///
ylabel(, angle(0))
graph export "${output}/ROC_rescaled_phase3.pdf", as(pdf) replace
restore

*By player DGP:
*DGP specific graphs:
levelsof playerdgp, local(levels) 
local count = 1
foreach l of local levels{
preserve
keep if playerdgp == "`l'"

collapse (mean) y, by(playertreatment_group x)

twoway (connected y x if playertreatment_group == "A", msymbol(Oh)) ///
(connected y x if playertreatment_group == "B", msymbol(Dh)) ///
(connected y x if playertreatment_group == "C", msymbol(Th)), ///
ytitle("") xtitle("")  legend(lab (1 "No Fit Line, Vertical Line") lab(2 "Fit Line, Vertical Line") lab(3 "No Fit Line, No Vertical Line")) title("DGP `count'", size(medsmall)) ///
 name(graph`count', replace) 


local count = `count' + 1
restore
}
grc1leg graph1 graph2 graph3 graph4 graph5 graph6 graph7 graph8 graph9 graph10 graph11, legendfrom(`1') title("Phase 3")
graph export "${output}/ROC_phase3_rescaled_byDGP.pdf", as(pdf) replace

graph close _all

*Phase 6
*-----------------

use "${dat}/Phase VI/survey_merged_PhaseVI.dta", clear
merge m:1 playerdgp using "`variances'", nogen
merge m:1 playerdgp using "`symmetry'", nogen

replace playerdisc = abs(playerdisc)
rename playerdisc disc

preserve
collapse (mean) y, by(playertreatment_group disc)
twoway (connected y disc if playertreatment_group == "A", msymbol(Oh)) ///
(connected y disc if playertreatment_group == "C", msymbol(Th)) ///
(connected y disc if playertreatment_group == "B", msymbol(Dh)) ///
(connected y disc if playertreatment_group == "D", msymbol(Sh)) ///
,legend(lab (1 "Small Bins, No Fit Line") lab(3 "Large Bins, No Fit Line") lab(2 "Small Bins, Fit Line") lab(4 "Large Bins, Fit Line"))  ///
ytitle("Share Reporting Discontinuity") xtitle("Discontinuity Magnitude") title("Phase 4") ///
ymtick(0.05) ymlabel("0.05", labsize(small) angle(0)) ///
ylabel(, angle(0))
graph export "${output}/ROC_phase6.pdf", as(pdf) replace
restore

* By symmetry of DGP support
preserve
keep if symmetric == 1
collapse (mean) y, by(playertreatment_group disc)
twoway (connected y disc if playertreatment_group == "A", msymbol(Oh)) ///
(connected y disc if playertreatment_group == "C", msymbol(Th)) ///
(connected y disc if playertreatment_group == "B", msymbol(Dh)) ///
(connected y disc if playertreatment_group == "D", msymbol(Sh)) ///
,legend(lab (1 "Small Bins, No Fit Line") lab(3 "Large Bins, No Fit Line") lab(2 "Small Bins, Fit Line") lab(4 "Large Bins, Fit Line"))  ///
ytitle("Share Reporting Discontinuity") xtitle("Discontinuity Magnitude") title("DGPs with Symmetric Supports") ///
ymtick(0.05) ymlabel("0.05", labsize(small) angle(0)) ///
ylabel(, angle(0))
graph export "${output}/ROC_phase6_symmetric_supports.pdf", as(pdf) replace
restore
preserve
keep if symmetric == 0
collapse (mean) y, by(playertreatment_group disc)
twoway (connected y disc if playertreatment_group == "A", msymbol(Oh)) ///
(connected y disc if playertreatment_group == "C", msymbol(Th)) ///
(connected y disc if playertreatment_group == "B", msymbol(Dh)) ///
(connected y disc if playertreatment_group == "D", msymbol(Sh)) ///
,legend(lab (1 "Small Bins, No Fit Line") lab(3 "Large Bins, No Fit Line") lab(2 "Small Bins, Fit Line") lab(4 "Large Bins, Fit Line"))  ///
ytitle("Share Reporting Discontinuity") xtitle("Discontinuity Magnitude") title("DGPs with Asymmetric Supports") ///
ymtick(0.05) ymlabel("0.05", labsize(small) angle(0)) ///
ylabel(, angle(0))
graph export "${output}/ROC_phase6_asymmetric_supports.pdf", as(pdf) replace
restore

*By player DGP:
*DGP specific graphs:
levelsof playerdgp, local(levels) 
local count = 1
foreach l of local levels{
preserve
keep if playerdgp == "`l'"

collapse (mean) y, by(playertreatment_group disc)

twoway (connected y disc if playertreatment_group == "A", msymbol(Oh)) ///
(connected y disc if playertreatment_group == "C", msymbol(Th)) ///
(connected y disc if playertreatment_group == "B", msymbol(Dh)) ///
(connected y disc if playertreatment_group == "D", msymbol(Sh)), ///
ytitle("") xtitle("")  legend(lab (1 "Small Bins, No Fit Line") lab(3 "Large Bins, No Fit Line") lab(2 "Small Bins, Fit Line") lab(4 "Large Bins, Fit Line")) title("DGP `count'", size(medsmall)) ///
 name(graph`count', replace)

local count = `count' + 1
restore
}
grc1leg graph1 graph2 graph3 graph4 graph5 graph6 graph7 graph8 graph9 graph10 graph11, legendfrom(`1') title("Phase 4")
graph export "${output}/ROC_phase6_byDGP.pdf", as(pdf) replace

graph close _all

* DGP9 only (for evaluating expert predictions of nonexpert performance)
preserve
keep if playerdgp == "IDGP9"
collapse (mean) y, by(playertreatment_group disc)
twoway (connected y disc if playertreatment_group == "A", msymbol(Oh)) ///
(connected y disc if playertreatment_group == "C", msymbol(Th)) ///
(connected y disc if playertreatment_group == "B", msymbol(Dh)) ///
(connected y disc if playertreatment_group == "D", msymbol(Sh)) ///
,legend(lab (1 "Small Bins, No Fit Line") lab(3 "Large Bins, No Fit Line") lab(2 "Small Bins, Fit Line") lab(4 "Large Bins, Fit Line"))  ///
ytitle("Share Reporting Discontinuity") xtitle("Discontinuity Magnitude") ///
ymtick(0.05) ymlabel("0.05", labsize(small) angle(0)) ///
ylabel(, angle(0))
graph export "${output}/ROC_phase6_DGP9.pdf", as(pdf) replace
restore

* Overall with color indicating expert-predicted performance
preserve
collapse (mean) y, by(playertreatment_group disc)
twoway (connected y disc if playertreatment_group == "A", lcolor(gs9) lstyle(solid) msymbol(i)) ///
(connected y disc if playertreatment_group == "C", lcolor(gs0) lstyle(solid) msymbol(i)) ///
(connected y disc if playertreatment_group == "B", lcolor(gs13) lstyle(solid) msymbol(i)) ///
(connected y disc if playertreatment_group == "D", lcolor(gs5) lstyle(solid) msymbol(i)) ///
,legend(lab (1 "Small Bins, No Fit Line (3)") lab(3 "Large Bins, No Fit Line (4)") lab(2 "Small Bins, Fit Line (1)") lab(4 "Large Bins, Fit Line (2)"))  ///
ytitle("Share Reporting Discontinuity") xtitle("Discontinuity Magnitude") 
graph export "${output}/ROC_phase6_expert_pred.pdf", as(pdf) replace
restore

* Overall with color indicating expert-predicted performance
preserve
keep if playerdgp == "IDGP9"
collapse (mean) y, by(playertreatment_group disc)
twoway (connected y disc if playertreatment_group == "A", lcolor(gs9) lstyle(solid) msymbol(i)) ///
(connected y disc if playertreatment_group == "C", lcolor(gs0) lstyle(solid) msymbol(i)) ///
(connected y disc if playertreatment_group == "B", lcolor(gs13) lstyle(solid) msymbol(i)) ///
(connected y disc if playertreatment_group == "D", lcolor(gs5) lstyle(solid) msymbol(i)) ///
,legend(lab (1 "Small Bins, No Fit Line (3)") lab(3 "Large Bins, No Fit Line (4)") lab(2 "Small Bins, Fit Line (1)") lab(4 "Large Bins, Fit Line (2)"))  ///
ytitle("Share Reporting Discontinuity") xtitle("Discontinuity Magnitude") 
graph export "${output}/ROC_phase6_DGP9_expert_pred.pdf", as(pdf) replace
restore

/*===========================================================
Create ROC curves using rescaled x axis:
=============================================================*/
destring dgp_sd, replace

gen asymp_sd = sqrt(asymp_var_rd_est)
gen abs_disc_cv = disc*dgp_sd
gen x = abs_disc_cv / asymp_sd
*-------------------------------------------
*Creating binned version of combined graph

count if x>0 & x<0.5
egen bins = cut(x), at(0 0.5(1)10.5)

*Obtain bin midpoints:
replace bins = bins + 0.5 if bins != . & bins ~= 0

preserve
keep if bins<=10
collapse (mean) y, by(playertreatment_group bins)
twoway (connected y bins if playertreatment_group == "A", msymbol(Oh)) ///
(connected y bins if playertreatment_group == "C", msymbol(Th)) ///
(connected y bins if playertreatment_group == "B", msymbol(Dh)) ///
(connected y bins if playertreatment_group == "D", msymbol(Th)) ///
,legend(lab (1 "Small Bins, No Fit Line") lab(3 "Large Bins, No Fit Line") lab(2 "Small Bins, Fit Line") lab(4 "Large Bins, Fit Line"))  ///
ytitle("Share Reporting Discontinuity") xtitle("Rescaled Discontinuity (Binned)") title("Phase 4") ///
ymtick(0.05) ymlabel("0.05", labsize(small) angle(0)) ///
ylabel(, angle(0))
graph export "${output}/ROC_rescaled_phase6.pdf", as(pdf) replace
restore

*By player DGP:
*DGP specific graphs:
levelsof playerdgp, local(levels) 
local count = 1
foreach l of local levels{
preserve
keep if playerdgp == "`l'"

collapse (mean) y, by(playertreatment_group x)

twoway (connected y x if playertreatment_group == "A", msymbol(Oh)) ///
(connected y x if playertreatment_group == "C", msymbol(Th)) ///
(connected y x if playertreatment_group == "B", msymbol(Dh)) ///
(connected y x if playertreatment_group == "D", msymbol(Th)), ///
ytitle("") xtitle("") legend(lab (1 "Small Bins, No Fit Line") lab(3 "Large Bins, No Fit Line") lab(2 "Small Bins, Fit Line") lab(4 "Large Bins, Fit Line")) title("DGP `count'", size(medsmall)) ///
 name(graph`count', replace) 

local count = `count' + 1
restore
}
grc1leg graph1 graph2 graph3 graph4 graph5 graph6 graph7 graph8 graph9 graph10 graph11, legendfrom(`1') title("Phase 4")
graph export "${output}/ROC_phase6_rescaled_byDGP.pdf", as(pdf) replace


* Phases 3 and 6 combined to look at the pooled effects of fit lines
use "${dat}/Phase VI/survey_merged_PhaseVI.dta", clear
gen phase = 6
keep if playertreatment_group == "A" | playertreatment_group == "C"
append using "${dat}/Phase III/survey_merged_PhaseIII.dta"
replace phase = 3 if phase == .
gen fitline = (phase == 6 & playertreatment_group == "C") | (phase == 3 & playertreatment_group == "B")

*Adjust the discontinuity levels to be correct.
replace playerdisc = abs(playerdisc)
rename playerdisc disc

preserve
collapse (mean) y, by(fitline disc)
twoway (connected y disc if fitline == 1, msymbol(Oh)) ///
(connected y disc if fitline == 0, msymbol(Dh)) ///
,legend(lab (1 "Fit line") lab(2 "No fit line"))  ///
ytitle("Share Reporting Discontinuity") xtitle("Discontinuity Magnitude") 
graph export "${output}/ROC_phase3_phase6_combined_fitlines.pdf", as(pdf) replace
restore

*Phase 7
*-----------------

use "${dat}/Phase VII/survey_merged_PhaseVII.dta", clear
merge m:1 playerdgp using "`variances'", nogen
merge m:1 playerdgp using "`symmetry'", nogen

replace playerdisc = abs(playerdisc)
rename playerdisc disc

preserve
collapse (mean) y, by(playertreatment_group disc)
twoway (connected y disc if playertreatment_group == "A", msymbol(Oh)) ///
(connected y disc if playertreatment_group == "C", msymbol(Th)) ///
(connected y disc if playertreatment_group == "B", msymbol(Dh)) ///
(connected y disc if playertreatment_group == "D", msymbol(Sh)) ///
,legend(lab (1 "Homosk. Global Quintic") lab(3 "Homosk. Local Linear") lab(2 "Heterosk. Global Quintic") lab(4 "Heterosk. Local Linear"))  ///
ytitle("Share Reporting Discontinuity") xtitle("Discontinuity Magnitude") title("Phase 5") ///
ymtick(0.05) ymlabel("0.05", labsize(small) angle(0)) ///
ylabel(, angle(0))
graph export "${output}/ROC_phase7.pdf", as(pdf) replace
restore

* By symmetry of DGP support
preserve
keep if symmetric == 1
collapse (mean) y, by(playertreatment_group disc)
twoway (connected y disc if playertreatment_group == "A", msymbol(Oh)) ///
(connected y disc if playertreatment_group == "C", msymbol(Th)) ///
(connected y disc if playertreatment_group == "B", msymbol(Dh)) ///
(connected y disc if playertreatment_group == "D", msymbol(Sh)) ///
,legend(lab (1 "Homosk. Global Quintic") lab(3 "Homosk. Local Linear") lab(2 "Heterosk. Global Quintic") lab(4 "Heterosk. Local Linear"))  ///
ytitle("Share Reporting Discontinuity") xtitle("Discontinuity Magnitude") title("DGPs with Symmetric Supports") ///
ymtick(0.05) ymlabel("0.05", labsize(small) angle(0)) ///
ylabel(, angle(0))
graph export "${output}/ROC_phase7_symmetric_supports.pdf", as(pdf) replace
restore
preserve
keep if symmetric == 0
collapse (mean) y, by(playertreatment_group disc)
twoway (connected y disc if playertreatment_group == "A", msymbol(Oh)) ///
(connected y disc if playertreatment_group == "C", msymbol(Th)) ///
(connected y disc if playertreatment_group == "B", msymbol(Dh)) ///
(connected y disc if playertreatment_group == "D", msymbol(Sh)) ///
,legend(lab (1 "Homosk. Global Quintic") lab(3 "Homosk. Local Linear") lab(2 "Heterosk. Global Quintic") lab(4 "Heterosk. Local Linear"))  ///
ytitle("Share Reporting Discontinuity") xtitle("Discontinuity Magnitude") title("DGPs with Asymmetric Supports") ///
ymtick(0.05) ymlabel("0.05", labsize(small) angle(0)) ///
ylabel(, angle(0))
graph export "${output}/ROC_phase7_asymmetric_supports.pdf", as(pdf) replace
restore

*By player DGP:
*DGP specific graphs:
levelsof playerdgp, local(levels) 
local count = 1
foreach l of local levels{
preserve
keep if playerdgp == "`l'"

collapse (mean) y, by(playertreatment_group disc)

twoway (connected y disc if playertreatment_group == "A", msymbol(Oh)) ///
(connected y disc if playertreatment_group == "C", msymbol(Th)) ///
(connected y disc if playertreatment_group == "B", msymbol(Dh)) ///
(connected y disc if playertreatment_group == "D", msymbol(Sh)), ///
ytitle("") xtitle("")  legend(lab (1 "Homosk. Global Quintic") lab(3 "Homosk. Local Linear") lab(2 "Heterosk. Global Quintic") lab(4 "Heterosk. Local Linear")) title("DGP `count'", size(medsmall)) ///
 name(graph`count', replace)

local count = `count' + 1
restore
}
grc1leg graph1 graph2 graph3 graph4 graph5 graph6 graph7 graph8 graph9 graph10 graph11, legendfrom(`1') title("Phase 5")
graph export "${output}/ROC_phase7_byDGP.pdf", as(pdf) replace

graph close _all

/*===========================================================
Create ROC curves using rescaled x axis:
=============================================================*/
destring dgp_sd, replace

gen asymp_sd = sqrt(asymp_var_rd_est)
gen abs_disc_cv = disc*dgp_sd
gen x = abs_disc_cv / asymp_sd
*-------------------------------------------
*Creating binned version of combined graph

count if x>0 & x<0.5
egen bins = cut(x), at(0 0.5(1)10.5)

*Obtain bin midpoints:
replace bins = bins + 0.5 if bins != . & bins ~= 0

preserve
keep if bins<=10
collapse (mean) y, by(playertreatment_group bins)
twoway (connected y bins if playertreatment_group == "A", msymbol(Oh)) ///
(connected y bins if playertreatment_group == "C", msymbol(Th)) ///
(connected y bins if playertreatment_group == "B", msymbol(Dh)) ///
(connected y bins if playertreatment_group == "D", msymbol(Th)) ///
,legend(lab (1 "Homosk. Global Quintic") lab(3 "Homosk. Local Linear") lab(2 "Heterosk. Global Quintic") lab(4 "Heterosk. Local Linear"))  ///
ytitle("Share Reporting Discontinuity") xtitle("Rescaled Discontinuity (Binned)") title("Phase 5") ///
ymtick(0.05) ymlabel("0.05", labsize(small) angle(0)) ///
ylabel(, angle(0))
graph export "${output}/ROC_rescaled_phase7.pdf", as(pdf) replace
restore

*By player DGP:
*DGP specific graphs:
levelsof playerdgp, local(levels) 
local count = 1
foreach l of local levels{
preserve
keep if playerdgp == "`l'"

collapse (mean) y, by(playertreatment_group x)

twoway (connected y x if playertreatment_group == "A", msymbol(Oh)) ///
(connected y x if playertreatment_group == "C", msymbol(Th)) ///
(connected y x if playertreatment_group == "B", msymbol(Dh)) ///
(connected y x if playertreatment_group == "D", msymbol(Th)), ///
ytitle("") xtitle("") legend(lab (1 "Homosk. Global Quintic") lab(3 "Homosk. Local Linear") lab(2 "Heterosk. Global Quintic") lab(4 "Heterosk. Local Linear")) title("DGP `count'", size(medsmall)) ///
 name(graph`count', replace) 


local count = `count' + 1
restore
}
grc1leg graph1 graph2 graph3 graph4 graph5 graph6 graph7 graph8 graph9 graph10 graph11, legendfrom(`1') title("Phase 5")
graph export "${output}/ROC_phase7_rescaled_byDGP.pdf", as(pdf) replace

*===============================================================================
*Princeton QSS experts (note they got big bins, unlike the rest of the experts)
*-----------------
use "${dat}/Expert Princeton/Expert Princeton part1", clear

replace playerdisc = abs(playerdisc)
rename playerdisc disc

preserve
collapse (mean) y, by(disc)
tempfile qss_col
save "`qss_col'", replace
restore

preserve
collapse (mean) y, by(disc)
twoway connected y disc, ///
	ytitle("Share Reporting Discontinuity") xtitle("Discontinuity Magnitude") ylab(0 (.2) 1) yscale(range(0 1))
graph export "${output}/ROC_expert_princeton_oct_2019.pdf", as(pdf) replace
restore

*By player DGP:
*DGP specific graphs:
levelsof playerdgp, local(levels) 
local count = 1
foreach l of local levels{
preserve
keep if playerdgp == "`l'"

collapse (mean) y, by(disc)

twoway connected y disc, ///
	ytitle("Share Reporting Discontinuity") xtitle("Discontinuity Magnitude") ylab(0 (.2) 1) yscale(range(0 1)) ///
	name(graphk`count', replace) ylab(0 (.2) 1) yscale(range(0 1)) title("DGP `count'", size(medsmall))
local count = `count' + 1
restore
}
graph combine graphk1 graphk2 graphk3 graphk4 graphk5 graphk6 graphk7 graphk8 graphk9 graphk10 graphk11
graph export "${output}/ROC_expert_princeton_oct_2019_byDGP_rd.pdf", as(pdf) replace

graph close _all

* Reweight nonexpert graphs so that the "distribution" of graphs seen matches these experts
gen count = 1
collapse (count) count, by(gid)
egen total = sum(count)
gen freq = count / total
keep gid freq
merge 1:m gid using "${dat}/survey_merged_rd_estimates_micro.dta"
keep if _merge == 3
collapse (mean) y [pweight = freq], by(abs_disc)
rename y y_nonexp_reweight
rename abs_disc disc
tempfile nonexp_reweight_col
save "`nonexp_reweight_col'"

* Create non-reweighted version
use "${dat}/survey_merged_rd_estimates_micro.dta", clear
keep if playertreatment_group == "B"
collapse (mean) y, by(abs_disc)
rename y y_nonexp
rename abs_disc disc
tempfile nonexp_col
save "`nonexp_col'"

use "`qss_col'", clear
merge 1:1 disc using "`nonexp_col'", nogen
merge 1:1 disc using "`nonexp_reweight_col'", nogen

twoway connected y y_nonexp_reweight y_nonexp disc, ///
	ytitle("Share Reporting Discontinuity") xtitle("Discontinuity Magnitude") ylab(0 (.2) 1) yscale(range(0 1)) ///
	legend(label(1 "Expert") label(2 "Non-Expert Reweighted") label(3 "Non-Expert"))
graph export "${output}/ROC_expert_qss_vs_nonexpert_reweighted.pdf", as(pdf) replace


*===============================================================================
*Experts combined: Cornell and Irvine seminars, all online waves
*-----------------
* Create pool of experts
use "${dat}/Expert Cornell/Expert Cornell part1.dta", clear
append using "${dat}/Expert Irvine/Expert Irvine part1.dta"
gen seminar = 1
append using "${dat}/Expert Jul 2019 Pilot/Expert Jul 2019 Pilot part1.dta"
append using "${dat}/Expert Aug 2019 Pilot/Expert Aug 2019 Pilot part1.dta"
append using "${dat}/Expert Aug 2019 First Round/Expert Aug 2019 First Round part1.dta"
replace seminar = 0 if seminar != 1

*Adjust the discontinuity levels to be correct.
replace playerdisc = abs(playerdisc)
rename playerdisc disc

preserve
collapse (mean) y, by(disc)
tempfile expert_col
save "`expert_col'", replace
twoway connected y disc, ///
	ytitle("Share Reporting Discontinuity") xtitle("Discontinuity Magnitude") ylab(0 (.2) 1) yscale(range(0 1))
graph export "${output}/ROC_expert_pooled_rd.pdf", as(pdf) replace
restore

* Reweight nonexpert graphs so that the "distribution" of graphs seen matches these experts
preserve
gen count = 1
collapse (count) count, by(gid)
egen total = sum(count)
gen freq = count / total
keep gid freq
merge 1:m gid using "${dat}/survey_merged_rd_estimates_micro.dta"
keep if _merge == 3
collapse (mean) y [pweight = freq], by(abs_disc)
rename y y_nonexp_reweight
rename abs_disc disc
tempfile nonexp_reweight_col
save "`nonexp_reweight_col'", replace

* Create non-reweighted version
use "${dat}/survey_merged_rd_estimates_micro.dta", clear
keep if playertreatment_group == "A"
collapse (mean) y, by(abs_disc)
rename y y_nonexp
rename abs_disc disc
tempfile nonexp_col
save "`nonexp_col'", replace

use "`expert_col'", clear
merge 1:1 disc using "`nonexp_col'", nogen
merge 1:1 disc using "`nonexp_reweight_col'", nogen

twoway connected y y_nonexp_reweight y_nonexp disc, ///
	ytitle("Share Reporting Discontinuity") xtitle("Discontinuity Magnitude") ylab(0 (.2) 1) yscale(range(0 1)) ///
	legend(label(1 "Expert") label(2 "Non-Expert Reweighted") label(3 "Non-Expert"))
graph export "${output}/ROC_expert_vs_nonexpert_reweighted.pdf", as(pdf) replace
restore

*By player DGP:
*DGP specific graphs:
levelsof playerdgp, local(levels) 
local count = 1
foreach l of local levels{
preserve
keep if playerdgp == "`l'"

collapse (mean) y, by(disc)

twoway connected y disc, ///
	ytitle("Share Reporting Discontinuity") xtitle("Discontinuity Magnitude") ylab(0 (.2) 1) yscale(range(0 1)) ///
	name(graphk`count', replace) ylab(0 (.2) 1) yscale(range(0 1)) title("DGP `count'", size(medsmall))
local count = `count' + 1
restore
}
graph combine graphk1 graphk2 graphk3 graphk4 graphk5 graphk6 graphk7 graphk8 graphk9 graphk10 graphk11
graph export "${output}/ROC_expert_pooled_byDGP_rd.pdf", as(pdf) replace

* Rescaled graphs using asymptotic t-stat as x-axis
merge m:1 playerdgp using "`variances'", nogen

gen asymp_sd = sqrt(asymp_var_rd_est)
gen abs_disc_cv = disc*sd
gen x = abs_disc_cv / asymp_sd

egen bins = cut(x), at(0 0.5(1)10.5)
replace bins = bins + 0.5 if bins != . & bins ~= 0

preserve
keep if bins <=10
collapse (mean) y, by(bins)
twoway connected y bins, ///
ytitle("Share Reporting Discontinuity") xtitle("Rescaled Discontinuity (Binned)") ylab(0 (.2) 1) yscale(range(0 1)) ///
	name(graphk`count', replace) ylab(0 (.2) 1) yscale(range(0 1))
graph export "${output}/ROC_rescaled_expert_pooled.pdf", as(pdf) replace
restore

*By player DGP:
*DGP specific graphs:
levelsof playerdgp, local(levels) 
local count = 1
foreach l of local levels{
preserve
keep if playerdgp == "`l'"

collapse (mean) y, by(bins)

twoway connected y bins, ///
	ytitle("Share Reporting Discontinuity") xtitle("Rescaled Discontinuity (Binned)") ylab(0 (.2) 1) yscale(range(0 1)) ///
	name(graphk`count', replace) ylab(0 (.2) 1) yscale(range(0 1)) title("DGP `count'", size(medsmall))
local count = `count' + 1
restore
}
graph combine graphk1 graphk2 graphk3 graphk4 graphk5 graphk6 graphk7 graphk8 graphk9 graphk10 graphk11
graph export "${output}/ROC_rescaled_expert_pooled_byDGP_rd.pdf", as(pdf) replace

graph close _all

log close

